package cn.hamster3.mc.plugin.core.bungee.api;

import cn.hamster3.mc.plugin.core.bungee.HamsterCorePlugin;
import cn.hamster3.mc.plugin.core.common.api.CoreAPI;
import cn.hamster3.mc.plugin.core.common.config.ConfigSection;
import cn.hamster3.mc.plugin.core.common.config.YamlConfig;
import cn.hamster3.mc.plugin.core.common.data.DisplayMessage;
import cn.hamster3.mc.plugin.core.common.impl.ComponentTypeAdapter;
import cn.hamster3.mc.plugin.core.common.impl.MessageTypeAdapter;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import net.kyori.adventure.platform.bungeecord.BungeeAudiences;
import net.kyori.adventure.text.Component;
import org.jetbrains.annotations.NotNull;

import java.io.File;
import java.io.IOException;
import java.util.logging.Logger;

@SuppressWarnings("unused")
public final class CoreBungeeAPI extends CoreAPI {
    @NotNull
    private final Gson gson;
    @NotNull
    private final Gson gsonHuman;

    private CoreBungeeAPI(@NotNull ConfigSection config) {
        super(config);
        gson = new GsonBuilder()
                .registerTypeAdapter(Component.class, ComponentTypeAdapter.INSTANCE)
                .registerTypeAdapter(DisplayMessage.class, MessageTypeAdapter.INSTANCE)
                .create();
        gsonHuman = new GsonBuilder()
                .registerTypeAdapter(Component.class, ComponentTypeAdapter.INSTANCE)
                .registerTypeAdapter(DisplayMessage.class, MessageTypeAdapter.INSTANCE)
                .serializeNulls()
                .setPrettyPrinting()
                .create();
    }

    public static CoreBungeeAPI getInstance() {
        return (CoreBungeeAPI) instance;
    }

    public static void init(@NotNull File configFile) throws IOException {
        if (instance != null) {
            return;
        }
        YamlConfig config = YamlConfig.load(configFile);
        instance = new CoreBungeeAPI(config);
    }

    @Override
    public @NotNull Logger getLogger() {
        return HamsterCorePlugin.getInstance().getLogger();
    }

    @Override
    public @NotNull Gson getGson() {
        return gson;
    }

    @Override
    public @NotNull Gson getGsonHuman() {
        return gsonHuman;
    }

    @Override
    public @NotNull BungeeAudiences getAudienceProvider() {
        return HamsterCorePlugin.getInstance().getAudienceProvider();
    }
}
